from celery import Celery
from exts import create_app
from config import window_host, REDIS_DB_URL

# broker = "redis://127.0.0.1:6379/1" # redis 1号库为任务执行队列
# backend = "redis://127.0.0.1:6379/2" # redis 2号库为任务执行结束后存下结果

# broker = f"redis://:{REDIS_DB_URL['password']}@{window_host}:6379/1" # redis 1号库为任务执行队列
# backend = f"redis://:{REDIS_DB_URL['password']}@{window_host}:6379/2" # redis 2号库为任务执行结束后存下结果
broker = f"redis://:{window_host}:6379/1" # redis 1号库为任务执行队列
backend = f"redis://:{window_host}:6379/2" # redis 2号库为任务执行结束后存下结果


# 包含的任务列表
include = ["celery_tasks.send_mail_task"]

# 构造装饰器，强化原本的celery
def make_celery(app):
    cel = Celery('default', broker=broker,backend=backend, include=include)
    class ContextTask(cel.Task):
        def __call__(self, *args, **kwargs):
            with app.app_context():
                return self.run(*args, **kwargs)
    cel.Task = ContextTask
    return cel

cel = make_celery(create_app()[0])


# 时区
cel.conf.timezone = "Asia/Shanghai"
# 是否使用UTC
cel.conf.enable_utc = False